<template>
	<view class="mine-page page">
		<CustomNav title="详情" :isBack="true"></CustomNav>
		<view class="info-block">
			<view class="info-list">
				<view class="info-list-title" style="font-size: 18px; font-weight: bold; color: #333;">报灾情况</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">灾害类型</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.typeText}}</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">村组</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.areaName}}</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">用户</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.username}}</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">用户手机</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.phone}}</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">位置</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.position}}</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">具体位置</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.specificPosition}}</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">灾害情况</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.disasterSituation}}</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">发生时刻</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.occurTime}}</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">图片/视频</view>
					</view>
					<view class="upload" v-if="info.pic!=undefined&&info.video!=undefined&&info.pic.length+info.video.length>0">
						<view v-for="(item,index) in info.pic" class="image-item">
							<image mode="aspectFit" :src="item" @click="previewImg(item)" class="img">
							</image>
						</view>
						<view v-for="(item,index) in info.video" class="image-item">
							<video id="myVideo" class="img" style="width: 190rpx;height: 200rpx;border-radius: 20rpx;"
								:src="item" @error="videoErrorCallback" show-fullscreen-btn></video>
						</view>
					</view>
					<view v-else>暂无</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">是否匿名</view>
					</view>
					<view class="form-content">
						<view class="form-crl" v-if="info.isAnonymous==1">是</view>
						<view class="form-crl" v-else>否</view>
					</view>
				</view>
			</view>
			<view class="info-list" v-if="info.disasterHandle">
				 <view class="info-list-title" style="font-size: 18px; font-weight: bold; color: #333;">灾害处理情况</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">办理人：</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.disasterHandle.name}}</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">办理时间：</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.disasterHandle.handleTime}}</view>
					</view>
				</view>	
				<view class="form-item">
					<view class="name-item">
						<view class="name">办理内容：</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.disasterHandle.handleText}}</view>
					</view>
				</view>	
				<view class="form-item">
						<view class="name-item">
							<view class="name">图片/视频</view>
						</view>
						<view class="upload" v-if="info.disasterHandle.urlList!=undefined&&info.disasterHandle.videoList!=undefined&&info.disasterHandle.urlList.length+info.disasterHandle.videoList.length>0">
							<view v-for="(item,index) in info.disasterHandle.urlList" class="image-item">
								<image mode="aspectFit" :src="item" @click="previewImg(item)" class="img">
								</image>
							</view>
							<view v-for="(item,index) in info.disasterHandle.videoList" class="image-item">
								<video id="myVideo" class="img" style="width: 190rpx;height: 200rpx;border-radius: 20rpx;"
									:src="item" @error="videoErrorCallback" show-fullscreen-btn></video>
							</view>
						</view>
						<view v-else>暂无</view>
				</view>
			</view>	
			<view class="info-list" v-if="info.claimsSettlementModel">
				<view class="info-list-title" style="font-size: 18px; font-weight: bold; color: #333;">理赔情况</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">理赔状态：</view>
					</view>
					<view class="form-content">
						<view class="form-crl" v-if="info.claimsSettlementModel.claimsSettlementStatus==1">已理赔</view>
						<view class="form-crl" v-else>未理赔</view>
					</view>
				</view>
				<view class="form-item">	
					<view class="name-item">
						<view class="name">处理人：</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.claimsSettlementModel.processingName}}</view>
					</view>
				</view>	
				<view class="form-item">
					<view class="name-item">
						<view class="name">理赔时间：</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.claimsSettlementModel.processingTime}}</view>
					</view>
				</view>
				<view class="form-item">		
					<view class="name-item">
						<view class="name">备注：</view>
					</view>
					<view class="form-content">
						<view class="form-crl">{{info.claimsSettlementModel.remark}}</view>
					</view>
				</view>	
				<view class="form-item">
				    <view class="name-item">
				        <view class="name">相关材料</view>
				    </view>
				   <!-- <view class="upload" v-if="info.claimsSettlementModel.fileList">
				        <view v-for="(item, index) in info.claimsSettlementModel.fileList" :key="index" class="file-item">
				            <view v-if="item.type === 'image'" class="image-item">
				                 <image mode="aspectFit" :src="item.url" @tap="previewImage(item.url)" class="img"></image>
				            </view>
							<view v-else-if="item.type === 'pdf'" class="pdf-item" @tap="previewPDF(item.url)">
				                <text class="pdf-name" style="color: blue">{{ item.name }}点击预览</text>
							</view>
				       </view>
				   </view> -->
				   <view class="file-list">
				         <template v-if="info.claimsSettlementModel.fileList">
				           <!-- 显示图片 -->
				           <view class="image-item" v-for="(item, index) in filteredItems('image')" :key="index">
				             <image mode="aspectFit" :src="item.url" @tap="previewImage(item.url)" class="img"></image>
				           </view>
				         </template>
				         <template v-if="info.claimsSettlementModel.fileList">
				           <!-- 显示 PDF -->
				           <view class="pdf-item" v-for="(item, index) in filteredItems('pdf')" :key="index" @tap="previewPDF(item.url)">
				             <text class="pdf-name" style="color: blue">{{ item.name }}(可点击预览)</text>
				           </view>
				         </template>
				         <template v-else>
				           <view>暂无</view>
				         </template>
				    </view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getDisasterById
	} from '@/api/user.js'
	export default {
		data() {
			return {
				info: {
					type: '',
					position: '',
					specificPosition: '',
					occurTime: '',
					disasterSituation: '',
					isAnonymous: 1,
					pic: [],
					video: []
				}
			}
		},
		methods: {
			//onShareAppMessage 分享给朋友
			//onShareTimeline  分享到朋友圈
			// #ifdef MP
			onShareAppMessage: function(res) {
				if (res.from === 'button') {
					// 来自页面内转发按钮
					console.log(res.target)
				}
				return {
					title: this.info.typeText,
					path: '/page_pack/user/record_detail?id=' + this.info.id
				}
			},
			// #endif
			onShareTimeline(res) {
				let distSource = uni.getStorageSync('distSource');
				if (distSource) {
					return {
						title: this.info.typeText,
						path: '/page_pack/user/record_detail?id=' + this.info.id,
						type: 0,
						query: 'id=' + distSource,
					}
				}
			},
			getDetail() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getDisasterById({
					id: this.id
				}).then(res => {
					this.info = res;
					console.log(this.info.claimsSettlementModel.fileList)
					if (this.info.url) {
						this.info.pic = this.info.url.split(',')
					}
					if (this.info.videoUrl) {
						this.info.video = this.info.videoUrl.split(',')
					}
				})
			},
			previewImg(current) {
				console.log(current)
				uni.previewImage({
					urls: [current],
				})
			},
			 previewImage(url) {
			    uni.previewImage({
			      urls: [url], // 这里需要传入图片的 url 列表
			    });
			  },
			  previewPDF(url) {
			      uni.downloadFile({
			        url: url,
			        success(res) {
			          const filePath = res.tempFilePath;
			          uni.openDocument({
			            filePath: filePath,
			            fileType: 'pdf',
			            success() {
			              console.log('打开文档成功');
			            },
			            fail(err) {
			              console.error('打开文档失败:', err);
			            },
			          });
			        },
			        fail(err) {
			          console.error('下载文件失败:', err);
			        },
			      });
			    },
				filteredItems(type) {
					console.log(this.info.claimsSettlementModel.fileList)
					 // 检查 this.info.claimsSettlementModel.fileList 是否为 undefined 或 null
					    if (!this.info.claimsSettlementModel || !this.info.claimsSettlementModel.fileList) {
					        return [];
					    }
				    // 过滤出对应类型的文件
				    return this.info.claimsSettlementModel.fileList.filter(item => item.type === type);
				},
		},
		onLoad(e) {
			this.id = e.id
			this.getDetail()
		}
	}
</script>

<style lang="scss" scoped>
	.mine-page {
		overflow: hidden;

		.info-block {
			margin-top: 23%;
			padding: 30rpx;
			background-color: #F7F6F9;
			min-height: 80vh;

			.info-list {
				background-color: #fff;
				border-radius: 20rpx;
				padding: 30rpx;
				margin-bottom: 20rpx;

				.title {
					width: 100%;
					font-size: 36rpx;
					font-weight: bold;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.form-item:last-child {
					border-bottom: none;
				}

				.form-item {
					display: flex;
					flex-direction: column;
					border-bottom: 1px solid #F7F7F7;
					padding-bottom: 30rpx;
					margin-top: 30rpx;

					.name-item {
						display: flex;
						align-items: center;
						margin-bottom: 30rpx;

						.name {
							font-size: 30rpx;
						}
					}

					.upload {
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;

						.image-item {
							margin-top: 20rpx;
							margin-right: 20rpx;
							display: flex;
							flex-direction: row;
							// box-sizing: border-box;
							// border: 1px solid #efefef;

							.img {
								width: 190rpx;
								height: 200rpx;
								border-radius: 20rpx;
							}

							.iconfont {
								.delete {
									width: 20px;
									height: 20px;
									position: absolute;
									margin-left: -30rpx;
								}
							}
						}

						.image-item:nth-child(3n) {
							margin-right: 0;
						}

						.delete-item {
							width: 100%;
							display: flex;
							justify-content: flex-end;

							.delete {
								width: 20px;
								height: 20px;
								position: absolute;
							}
						}

						.image-item1 {
							display: flex;
							flex-direction: row;

							.delete {
								margin-top: 10rpx;
								margin-left: -40rpx;
								width: 20px;
								height: 20px;
								position: absolute;
							}
						}

						.upload-content {
							position: relative;
							background: #fafafa;
							border-radius: 5px;
							width: 200rpx;
							height: 200rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							margin-top: 20rpx;

							.title {
								position: absolute;
								top: 50%;
								left: 50%;
							}

							.upload {
								width: 90rpx;
								height: 90rpx;
							}
						}
					}

					.form-content {
						display: flex;
						align-items: center;
						justify-content: space-between;
						width: 100%;

						.form-crl1 {
							width: calc(100% - 140rpx);
						}

						.form-crl {
							width: 100%;
						}

						.right {
							display: flex;
							align-items: center;

							.iconfont {
								margin-right: 10rpx;
								color: #3466FF;
								font-size: 32rpx;
							}

							.distance {
								color: #767676;
								font-size: 28rpx;
							}
						}
					}
				}
			}
		}
	}
	.info-list-title {
	    font-size: 18px;
	    font-weight: bold;
	    color: #333;
	}
	.file-list {
	  display: flex;
	  flex-wrap: wrap;
	}
	
	.image-item {
	  width: 50%; /* 控制图片宽度，根据需要调整 */
	}
	
	.pdf-item {
	  width: 100%; /* 控制 PDF 宽度，根据需要调整 */
	  line-height: 2; /* 设置行间距，可以根据需要调整，1 表示默认行高 */
	}
	
	.img {
	  width: 100%; /* 图片宽度充满父容器 */
	}
	
	.pdf-name {
	  color: blue;
	}
</style>